package com.jml.泛型;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: J
 * @DATE: 2023-02-15 11:00
 */
public class Main {
    /**
     * <? extends Person>
     *
     * @param args
     */
    public static void main1(String[] args) {
        /**
         *数组中使用
         */
        List<? extends Person> ld = new ArrayList<>();
        //会编译报错
        /*ld.add(new Driver());
        ld.add(new Person());
        ld.add(new Object());*/
        //不会编译报错
        ld.get(0);

        /**
         *方法参数传递,只能传Person本身或者他的子类，就是继承了Person的类，不能是父类
         */
        Main main = new Main();
        //不报错
        List<Driver> driverList = new ArrayList();
        main.top(driverList);
        List<Person> personList = new ArrayList();
        main.top(personList);
        //报错
        /*List<Object> objectList = new ArrayList();
        main.top(objectList);*/
    }

    public void top(List<? extends Person> a) {
    }

    /**
     * <? super Person>
     *
     * @param args
     */
    public static void main(String[] args) {
        List<? super Driver> ld = new ArrayList<>();
        //ld.add(new Person());
        ld.add(new Driver());
        ld.add(new DriverSon());
        ld.get(0);
        /**
         * 报错
         */
        //ld.add(new Object());
        /**
         *方法参数传递,只能传Person本身或者他的子类，就是继承了Person的类，不能是父类
         */
        Main main = new Main();
        //不报错
        List<Driver> driverList = new ArrayList();
        main.top1(driverList);
        List<Person> personList = new ArrayList();
        main.top1(personList);
        List<Object> objectList = new ArrayList();
        main.top1(objectList);
        //报错,不能放子类
        List<DriverSon> DriverSonList = new ArrayList<>();
        //main.top1(DriverSonList);
    }

    public void top1(List<? super Driver> a) {
        a.get(0);
        a.add(new Driver());
    }


}
